package scc.com.shuiyin;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

/**
 * Created by DELL on 2017/8/22.
 */
public class BitMapUtils {

    private static float defaultTextSize = 40;//默认文字大小

    /**
     * 根据字符串生成bitmap
     *
     * @param str
     * @param color 文字颜色
     * @param size  文字大小
     * @return
     */
    public static Bitmap getBitmapByString(String str, int color, float size) {

        //计算图片宽高
        int length = str.length();
        if (size != -1) defaultTextSize = size;
        int width = (int) (defaultTextSize * length);
        int height = (int) defaultTextSize + 6;
        Log.d("ssssss", "getBitmapByString: "+width+"----"+height);
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setStrokeWidth(10.0f);
        paint.setAntiAlias(true);
        paint.setTextSize(defaultTextSize);
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawText(str, 0, str.length(), 0, defaultTextSize, paint);
        if (bitmap != null) return bitmap;
        return null;
    }

    public static Bitmap getBitmapByString(String str) {
        Bitmap bitmap = getBitmapByString(str, Color.BLUE, defaultTextSize);
        if (bitmap != null) return bitmap;
        return null;
    }
}
